unit config;

interface

uses
  System.Classes, System.SysUtils, System.Variants, Winapi.Windows,
  Winapi.Messages;

type
  TConfigs = class
  private
    _popupMessages: boolean;
    _globalVolume: integer;
    _retryError: boolean;
    function getStartup: boolean;
    procedure setStartup(const Value: boolean);

  public
   constructor Create();

    procedure Load();
    procedure Save();
    property PopupMessages: boolean read _popupMessages write _popupMessages;
    property GlobalVolume: integer read _globalVolume write _globalVolume;
    property RetryError: boolean read _retryError write _retryError;
    property IsStartup: boolean read getStartup write setStartup;
  end;

implementation

uses inifiles, registry;

{ TConfigs }

constructor TConfigs.Create;
begin
 _popupMessages:=true;
 _retryError:=true;
{$IFDEF DEBUG}
  _globalVolume := 20;
{$ELSE}
  _globalVolume := 200;
{$ENDIF}
end;

function TConfigs.getStartup: boolean;
var
  reg: TRegistry;
begin
  Result := false;
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if (reg.OpenKeyReadOnly('Software\Microsoft\Windows\CurrentVersion\Run'))
    then
      Result := reg.ValueExists('MyRadio');
  finally
    FreeAndNil(reg);
  end;
end;

procedure TConfigs.Load;
var
  ini: TIniFile;
  f: string;
begin
  f := ExtractFilePath(ParamStr(0)) + 'MyRadio.ini';
  if (not FileExists(f)) then
    Exit();
  ini := TIniFile.Create(f);
  try
    _globalVolume := ini.ReadInteger('GlobalVolume', 'Value', _globalVolume);
    _popupMessages := ini.ReadBool('Normal', 'PopupMessages', true);
    _retryError := ini.ReadBool('Normal', 'RetryForError', true);

  finally
    FreeAndNil(ini);
  end;

end;

procedure TConfigs.Save;
var
  ini: TIniFile;
  f: string;
begin
  f := ExtractFilePath(ParamStr(0)) + 'MyRadio.ini';
  ini := TIniFile.Create(f);
  try
    ini.WriteInteger('GlobalVolume', 'Value', _globalVolume);
    ini.WriteBool('Normal', 'PopupMessages', _popupMessages);
    ini.WriteBool('Normal', 'RetryForError', _retryError);

  finally
    FreeAndNil(ini);
  end;

end;

procedure TConfigs.setStartup(const Value: boolean);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CURRENT_USER;
    if (reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', false))
    then
      if (Value) then
        reg.WriteString('MyRadio', Format('"%s"', [ParamStr(0)]))
      else if reg.ValueExists('MyRadio') then
        reg.DeleteValue('MyRadio')
  finally
    FreeAndNil(reg);

  end;

end;

end.
